<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="/static/layui/css/layui.css">
</head>

<body>
  <div style="padding-left: 20px;padding-top: 20px;">
    <span class="layui-breadcrumb">
      <a href="/dashBoard">首页</a>
      <a href="/itemList">商品列表</a>
      <a><cite>增加商品</cite></a>
    </span>
  </div>
  <form class="layui-form" action="" style="margin-top:20px;max-width: 800px">
    <div class="layui-form-item">
      <label class="layui-form-label">商品名称</label>
      <div class="layui-input-block">
        <input type="text" name="name" required lay-verify="required" placeholder="请输入商品名称" autocomplete="off"
          class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">商品价格</label>
      <div class="layui-input-block">
        <input type="number" name="price" required lay-verify="required" placeholder="请输入商品价格" autocomplete="off"
          class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">商品库存</label>
      <div class="layui-input-block">
        <input type="number" name="store" placeholder="请输入商品库存" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">上下架</label>
      <div class="layui-input-block">
        <input type="checkbox" name="onSale" lay-text="上架|下架" lay-skin="switch">
      </div>
    </div>
    <div class="layui-form-item layui-form-text">
      <label class="layui-form-label">商品描述</label>
      <div class="layui-input-block">
        <textarea name="desc" placeholder="请输入描述" class="layui-textarea"></textarea>
      </div>
    </div>
    <div class="layui-form-item layui-form-text">
      <label class="layui-form-label">封面图片</label>
      <div class="layui-input-block">
        <button type="button" class="layui-btn" id="upload">
          <i class="layui-icon">&#xe67c;</i>上传图片
        </button>
        <img src="" id="img" alt="">
      </div>
    </div>
    <div class="layui-form-item layui-form-text">
      <label class="layui-form-label">内容</label>
      <div class="layui-input-block">
        <div id="content"></div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn" lay-submit lay-filter="itemAdd">立即提交</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
      </div>
    </div>
  </form>
  <script src="/static/layui/layui.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/wangeditor@latest/dist/wangEditor.min.js"></script>
  <script>
    //Demo
    layui.use(['form', 'jquery', 'layer', 'upload'], function () {
      var form = layui.form;
      var $ = layui.jquery;
      var layer = layui.layer;
      var upload = layui.upload;
      var thumb = '';
      // 初始化富文本控件
      const E = window.wangEditor
      const editor = new E('#content')
      editor.create()
      // 初始化 上传文件控件
      //执行实例
      var uploadInst = upload.render({
        elem: '#upload' //绑定元素
        , field: 'file'
        , url: '/upload' //上传接口
        , done: function (res) {
          //上传完毕回调
          thumb = res.data.url;
          $('#img').attr('src', res.data.url)
        }
        , error: function () {
          //请求异常回调
          console.log('上传失败');
        }
      });
      //监听提交
      form.on('submit(itemAdd)', function (data) {
        // 提交表单新增商品
        // 处理上下架
        if (data.field.onSale === 'on') {
          // 开关打开状态 上架
          data.field.onSale = '1';
        } else {
          data.field.onSale = '0';
        }

        data.field.thumb = thumb;
        // 得到富文本输入的内容  合并到提交 data.field中
        
        data.field.content = editor.txt.html();
        $.ajax({
          url: '/itemAdd',
          type: 'post',
          data: data.field,
          success: function (res) {
            if (res.code === 200) {
              // 新增成功弹出提示
              layer.msg(res.msg, { icon: 1 }, function () {
                // 刷新页面清空表单，方便下次新增商品
                history.go(0)
              })
            } else {
              layer.msg(res.msg, { icon: 2 })
            }
          }
        }) 
        // 让你自己用ajax进行提交
        return false;
      });
    });
  </script>
</body>

</html>